package com.lhc.controller;

import com.lhc.dataObject.OrderDo;
import com.lhc.error.BusniessException;
import com.lhc.error.EmBussinessError;
import com.lhc.response.CommonReturnType;
import com.lhc.service.OrderService;
import com.lhc.service.model.OrderModel;
import com.lhc.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller("order")
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class OrderController extends BaseController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/createorder", method = RequestMethod.POST)
    @ResponseBody
    public CommonReturnType createOrder(@RequestParam("itemId") Integer itemId,
                                        @RequestParam("account") Integer account) throws BusniessException {
        Boolean isLogin = (Boolean) this.request.getSession().getAttribute("IS_LOGIN");
        if (isLogin == null || !isLogin.booleanValue()) {
            throw new BusniessException(EmBussinessError.PARAMETER_VALIDATION_ERROR, "用户未登录");
        }

        UserModel userModel = (UserModel) this.request.getSession().getAttribute("LOGIN_USER");

        OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, account);

        return CommonReturnType.create(null);

    }

}
